layui.extend({
    baseConfig : "/js/pages/baseConfig"
})
layui.use(['form','layer','laydate','table','laytpl','element','util','baseConfig'],function(){

    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        baseConfig = layui.baseConfig,
        table = layui.table;
    var util = layui.util;
    var tableIns = table.render({
        elem: '#list',
        url : '/depanPerso/list/',
        text: { none: "Aucun résultat"},
        cellMinWidth : 95,
        height : "full-125",
        id : "listTable",
        cols : [[
            {field: 'date', title: "Date", align:"center", templet : function( d){
                return baseConfig.formatDate( d.date);
            }},
            {field: 'cooperativeName', title: "Contrepartie", align:'center'},
            {field: 'jobNumber', title: "ID", align:'center'},
            {field: 'methodeName', title: "Méthode", align:'center', templet: function ( d) {
                return "<div style=\"overflow:hidden;white-space:nowrap;text-overflow:ellipsis;\">" + d.methodeName + "</div>";
            }},
            {field: 'memo', title: "Remarqe", align:'center', templet: function ( d) {
                return "<div style=\"overflow:hidden;white-space:nowrap;text-overflow:ellipsis;\">" + d.memo + "</div>";
            }},
            {field: 'status', title: "Etat", align:'center', templet : function( d){
                switch ( d.status) {
                    case "1" : return "已到账";
                    case "2" : return "等待到账中";
                }
            }},
            {field: 'cooperativeNumber', title: "供应商编号", align:'center'},
            {field: 'montant', title: "Montant", align:'center', templet : function( d){
                return parseFloat( d.montant).toFixed(2);
            }},
            {field: 'createTime', title: '添加时间', align:'center', templet : function( d){
                return baseConfig.formatDate( d.createTime);
            }},
            {title: 'État', fixed: "right", align:"center", templet : function ( d) {
                var str = "";
                if( d.status == "1"){
                    str = "<a class=\"layui-btn layui-btn-xs\" lay-event=\"detail\">查看</a>";
                }else{
                    str = "<a class=\"layui-btn layui-btn-xs\" lay-event=\"edit\">修改</a>";
                }
                return str;
            }}
        ]],
        response : {
            statusCode: 200 //规定成功的状态码，默认：0
        },
        parseData : function( res){ //res 即为原始返回的数据
            return {
                "code": res.code, //解析接口状态
                "msg": res.msg, //解析提示文本
                "data": res.data //解析数据列表
            };
        }
    });
    //获取浏览器最大高度，以防页面显示不完全
    var maxHeight = $(window).height();

    //添加/修改/详细查看（readonly=true）
    function addOrUpdateOrDetail( data, readonly){
        var index = layui.layer.open({
            title : "添加",
            type : 2,
            area: [800 + 'px', (maxHeight < 600 ? maxHeight-10 : 600) + 'px'],
            shade: 0.4,
            btnAlign: 'r',
            id: "depanPerso", //设定一个id，防止重复弹出
            content : "formData.html?cooperativeNumber=" + data ? data.cooperativeNumber : "",
            success : function(){
                var body = layui.layer.getChildFrame( 'body', index);
                body.find( "input[name=contrepartie]").val( "NUXUA").attr( "readonly", true);//标识是修改还是新增
                if( data){
                    body.find( "#methodeId").val( data.methode);
                    body.find( "input[name=id]").val( data.id).attr( "readonly", readonly);//标识是修改还是新增
                    body.find( "#jobNumberId").val( data.jobNumber);

                    body.find( "input[name=date]").val( baseConfig.formatDate( data.date)).attr( "readonly", readonly);
                    body.find( "input[name=memo]").val( data.memo).attr( "readonly", readonly);
                    body.find( "input[name=montant]").val( data.montant).attr( "readonly", readonly);
                    if( readonly){
                        body.find( "#submitDiv").hide();
                    }
                    form.render();
                }
                setTimeout( function(){
                    layui.layer.tips('点击此处返回数据列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500);
            }
        });
    }

    //添加
    $(".add_btn").click(function(){
        addOrUpdateOrDetail();
    });

    //列表操作
    table.on('tool(list)', function(obj){
        var layEvent = obj.event,
            data = obj.data;
        if(layEvent === 'edit'){ //编辑
            addOrUpdateOrDetail( data, false);
        }else if(layEvent === 'detail'){ //编辑
            addOrUpdateOrDetail( data, true);
        }
    });
});
